package com.yunheone.control;

import com.yunheone.Jdbc.JdbcHelp;
import com.yunheone.mode.User;
import com.yunheone.view.user.AddUser;
import com.yunheone.view.user.DellUser;
import com.yunheone.view.user.UserList;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 已登录的用户内容的处理
 *
 */
public class Usercontrol {

    /**
     * 负责用户相关功能的跳转控制器
     */
    public static void listrout(){
        int i= UserList.list ();//展示用户列表，并拿到返回值
        switch (i){
            case 1:adduser ();break;//添加用户
            case 2:Update ();break;//修改用户
            case 3:remove ();break;//删除用户
            case 4:Admincontrol.getInstance ().Rount ();break;//返回用户页主页
            case 5:listrout ();break;//刷新
        }
    }

    /**
     * 管理员添加用户信息
     */
    public static void adduser(){
        int a=2;
        User add = AddUser.add ();
        int i = chat (  add.getId (), add.getType () );
        if (i==1){
            System.out.println ("该用户已存在！");
        }else{
            if (add.getType ().equals ( "管理员" )){
                a=1;
            }
            String sql="insert into user(no,useName,passWord,type,valName,type_id) values(?,?,?,?,?,?)";
            PreparedStatement item = JdbcHelp.item ( sql, add.getId (), add.getUseName (), add.getPassWord (), add.getType (), add.getValName (),a );
            JdbcHelp.Update ( item );
            JdbcHelp.close ( item );
        }
        listrout ();
    }
//    删除
    public static void remove(){
        User delluser = DellUser.delluser ();
        int i = chat (  delluser.getId (), delluser.getType () );
        if (i==0){
            System.out.println ("该用户不存在！");
        }else {
            String sql = "delete from user where no=? and type=?";
            PreparedStatement item = JdbcHelp.item ( sql, delluser.getId (), delluser.getType () );
            JdbcHelp.Update ( item );
            JdbcHelp.close ( item );
        }
        listrout ();
    }

    /**
     * 修改
     */
    public static void Update(){
            User add = AddUser.add ();
            int a=2;
            if(add.getType ().equals ( "管理员" )){
                a=1;
             }
            String sql="update  user set  useName=? , passWord=? , type=? , valName=? ,type_id=? where no=?";
            PreparedStatement item = JdbcHelp.item ( sql, add.getUseName (), add.getPassWord (), add.getType (), add.getValName (), a,add.getId () );
            JdbcHelp.Update ( item );
            JdbcHelp.close ( item );
            listrout ();
    }



    /**
     * 判断表中是否存在type下的name，存在返回1.不存在返回0
     *
     * @param
     * @param type
     * @return
     */
    public static int  chat(String no ,String type){
        int i=0;
        String s="select * from user where no=? and type=?";
        PreparedStatement item = JdbcHelp.item ( s,no,type);
        ResultSet query = JdbcHelp.Query ( item );
        try {
            if (query.next ()){
                i=1;
            }
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return i;
    }
}
